home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / srcbkvt.zip / HTMLPP.ASC < prev    next >
Text File  |  1996-07-08  |  7KB  |  257 lines

  1. _The Libero Development Environment_
  2. by Pieter Hintjens
  3.  
  4. Listing One
  5. /^\.\w+\s+([A-Za-z0-9-\._]+)\s+(.*)/
  6. $symbols {$1} = $2;
  7.  
  8. Listing Two
  9.  
  10. (a)
  11. -schema=lrschema.pl
  12.  
  13. After-Init:
  14.     (--) Ok                                 -> Have-Line
  15.           + Initialise-Program-Data
  16.           + Open-Main-Document
  17.           + Get-Next-Document-Line
  18.     (--) Error                              ->
  19.           + Terminate-The-Program
  20.  
  21.  
  22. (b)
  23.  
  24. require 'htmlpp1.d';                    #   Include dialog interpreter
  25.  
  26. sub initialise_the_program
  27. {
  28.     print "htmlpp 1.0 - by Pieter Hintjens\n";
  29.  
  30.     if ($#ARGV == 0) {                  #   Exactly 1 argument in @ARGV[0]?
  31.         $main_document = @ARGV [0];
  32.         $the_next_event = $ok_event;
  33.     } else {
  34.         print "syntax: htmlpp <filename>\n";
  35.         $the_next_event = $error_event;
  36.     }
  37. }
  38.  
  39. Listing Three
  40.  
  41. (a)
  42.  
  43. sub initialise_program_data
  44. {
  45.     #   These are the preprocessor keywords that we recognise
  46.     $keyword {"define"}  = $define_event;
  47.     $keyword {"include"} = $include_event;
  48. }
  49.  
  50. (b)
  51.  
  52. sub open_main_document
  53. {
  54.     $document = $main_document;
  55.     &open_the_document;
  56. }
  57.  
  58. sub open_the_document
  59.  
  60. {
  61.     #   We use an indirect filehandle, whose name is the document name.
  62.     #   To read from the file, we use <$document>
  63.     if (open ($document, $document)) {
  64.         $file_is_open {$document} = 1;  #   Keep track of open documents
  65.     } else {
  66.         print "htmlpp E: ($document $.) can't open $document: $!";
  67.         &raise_exception ($exception_event);
  68.     }
  69. }
  70.  
  71. Listing Four
  72. open (HANDLE, "somefile.txt");
  73. $line = <HANDLE>;
  74.  
  75. Listing Five
  76. sub get_next_document_line
  77. {
  78.     if ($_ = <$document>) {             #   Get next line of input
  79.         chop;                           #   Remove trailing newline
  80.         if (/^$/) {                     #   Blank lines
  81.             $the_next_event = $blank_line_event;
  82.         }
  83.         elsif (/^\.-/) {                #   Comments
  84.             $the_next_event = $comment_event;
  85.         }
  86.         elsif (/^\./) {                 #   Line starts with a dot
  87.             /^\.(\w+)/;                 #   Get word after dot
  88.             if (defined ($keyword {$1})) {
  89.                 $the_next_event = $keyword {$1};
  90.             } else {
  91.                 &syntax_error;
  92.             }
  93.         } else {
  94.             $the_next_event = $body_text_event;
  95.         }
  96.     } else {
  97.         $the_next_event = $finished_event;
  98.     }
  99. }
  100.  
  101. Listing Six
  102. Have-Line:
  103.     (--) Body-Text                          -> Have-Line
  104.           + Expand-Symbols-In-Line
  105.           + Get-Next-Document-Line
  106.     (--) Blank-Line                         -> Have-Line
  107.           + Get-Next-Document-Line
  108.     (--) Comment                            -> Have-Line
  109.           + Get-Next-Document-Line
  110.     (--) Define                             -> Have-Line
  111.           + Expand-Symbols-In-Line
  112.           + Store-Symbol-Definition
  113.           + Get-Next-Document-Line
  114.     (--) Include                            -> Have-Line
  115.  
  116.           + Expand-Symbols-In-Line
  117.           + Take-Include-File-Name
  118.           + Open-The-Document
  119.           + Get-Next-Document-Line
  120.     (--) Finished                           -> Doc-Unstacked
  121.           + Close-The-Document
  122.           + Unstack-Previous-Document
  123.  
  124. Listing Seven
  125.     (--) Define                             -> Have-Line
  126.           + Expand-Symbols-In-Line
  127.           + Store-Symbol-Definition
  128.           + Get-Next-Document-Line
  129.  
  130.  
  131. Listing Eight
  132. sub store_symbol_definition
  133. {
  134.     #   Symbol name can consist of letters, digits, -._
  135.     #   We re-parse the line to extract the symbol name and value:
  136.  
  137.     if (/^\.\w+\s+([A-Za-z0-9-\._]+)\s+(.*)/) {
  138.         #   Stick name and value into associative array '$symbols'
  139.         $symbols {$1} = $2;
  140.     } else {
  141.         &syntax_error;
  142.     }
  143. }
  144.  
  145. Listing Nine
  146. sub expand_symbols_in_line
  147. {
  148.     #   Expands symbols in $_ variable
  149.     #
  150.     #   Repeatedly expand symbols like this:
  151.     #   $(xxx) - value of variable
  152.     #
  153.     #   Note that the entire symbol must be on one line; if the symbol or
  154.     #   its label is broken over two lines it won't be expanded.
  155.  
  156.     for (;;) {
  157.         if (/\$\(([A-Za-z0-9-_\.]+)\)/) {
  158.             $_ = $`.&valueof ($1).$';
  159.         } else {
  160.             last;
  161.         }
  162.     }
  163. }
  164. #   Subroutine returns the value of the specified symbol; it issues a
  165. #   warning message and returns 'UNDEF' if the symbol is not defined.
  166. #
  167. sub valueof {
  168.     local ($symbol_name) = "@_";        #   Argument is symbol name
  169.  
  170.     defined ($symbols {$symbol_name}) && return $symbols {$symbol_name};
  171.  
  172.     print "$_\n";
  173.     print "htmlpp E: ($document $.) undefined symbol \"$symbol_name\"\n";
  174.     $symbols {$symbol_name} = "UNDEF";
  175.     return $symbols {$symbol_name};
  176. }
  177.  
  178. Listing Ten
  179. (a)
  180.  
  181. sub syntax_error {
  182.     print "$_\n";
  183.     print "htmlpp E: ($document $.) syntax error\n";
  184.     &raise_exception ($exception_event);
  185. }
  186.  
  187.  
  188. (b)
  189.  
  190. Defaults:
  191.     (--) Exception                          ->
  192.           + Terminate-The-Program
  193.  
  194. Listing Eleven
  195. (a) 
  196.  
  197. sub take_include_file_name
  198. {
  199.     #   Get filename after .include
  200.     if (/^\.\w+\s+(\S+)/) {
  201.         if ($file_is_open {$1}) {
  202.             print "$_\n";
  203.             print "htmlpp E: ($document $.) $1 is already open";
  204.             &raise_exception ($exception_event);
  205.         };
  206.         #   Save current document name and switch to new document
  207.         push (@document_stack, $document);
  208.         $document = $1;
  209.     } else {
  210.         &syntax_error;
  211.     }
  212. }
  213.  
  214. (b)
  215.  
  216.     (--) Finished                           -> Doc-Unstacked
  217.           + Close-The-Document
  218.           + Unstack-Previous-Document
  219.  
  220. sub close_the_document
  221. {
  222.     close ($document);
  223.     undef $file_is_open {$document};
  224. }
  225.  
  226. sub unstack_previous_document
  227.  
  228. {
  229.     $document = pop (@document_stack);
  230.     $the_next_event = $document eq ""? $finished_event: $ok_event;
  231. }
  232.  
  233.  
  234. Listing Twelve
  235. Doc-Unstacked:
  236.     (--) Ok                                 -> Have-Line
  237.           + Get-Next-Document-Line
  238.     (--) Finished                           -> Have-Line
  239.           + Terminate-The-Program
  240.  
  241. Figure 2:
  242.  
  243. .include prelude.def
  244. .define version 2.12
  245. .page lrintro.htm=Introduction to Libero
  246. .ignore
  247. <H1>$(TITLE)</H1>
  248. .include contents.def
  249. <H2>Summary</H2>
  250. <UL>
  251. <LI>Libero is a Programmer's Tool and Code Generator.</LI>
  252. <LI>It supports lots of languages.</LI>
  253. <LI>It runs on lots of systems.</LI>
  254. <LI>Current version: $(version).</LI>
  255. </UL>
  256.  
  257.